package com.ayu.chatroom.util;

//import com.github.tobato.fastdfs.domain.fdfs.StorePath;
//import com.github.tobato.fastdfs.exception.FdfsUnsupportStorePathException;
//import com.github.tobato.fastdfs.service.FastFileStorageClient;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

//import org.apache.commons.lang3.StringUtils;

@Component
public class FastDFSClient {

//	@Autowired
//	private FastFileStorageClient storageClient;

//	@Autowired
//	private AppConfig appConfig; // 项目参数配置



	@Value("${aliyun.oss.file.endpoint:}")
	private String endpoint;

	@Value("${aliyun.oss.file.accesskeyid:}")
	private String keyId;

	@Value("${aliyun.oss.file.accesskeysecret:}")
	private String keySecret;

	@Value("${aliyun.oss.file.bucketname:}")
	private String bucketName;


	public String uploadAvatar(MultipartFile file)
	{

		// 创建OSS实例。
		OSS ossClient = new OSSClientBuilder().build(endpoint, keyId, keySecret);

		try
		{

			//获取上传文件输入流
			InputStream inputStream = file.getInputStream();
			//获取文件名称
			String fileName = file.getOriginalFilename();

			//1 在文件名称里面添加随机唯一的值
			String uuid = UUID.randomUUID().toString().replaceAll("-", "");
			// yuy76t5rew01.jpg
			fileName = uuid + fileName;

			//2 把文件按照日期进行分类
			//获取当前日期
			//   2019/11/12
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
			String datePath = sdf.format(new Date());
			//拼接
			//  2019/11/12/ewtqr313401.jpg
			fileName = datePath + "/" + fileName;

			//调用oss方法实现上传
			//第一个参数  Bucket名称
			//第二个参数  上传到oss文件路径和文件名称   aa/bb/1.jpg
			//第三个参数  上传文件输入流
			ossClient.putObject(bucketName, fileName, inputStream);

			//把上传之后文件路径返回
			//需要把上传到阿里云oss路径手动拼接出来
			//  https://edu-guli-1010.oss-cn-beijing.aliyuncs.com/01.jpg
			String url = "https://" + bucketName + "." + endpoint + "/" + fileName;
			return url;
		} catch (Exception e)
		{
			e.printStackTrace();
			return null;
		} finally
		{
			// 关闭OSSClient。
			ossClient.shutdown();

		}

	}

	/**
	 * 上传文件
	 * 
	 * @param file
	 *            文件对象
	 * @return 文件访问地址
	 * @throws IOException
	 */
//	public String uploadFile(MultipartFile file) throws IOException {
//		StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(),
//				FilenameUtils.getExtension(file.getOriginalFilename()), null);
//
//		return storePath.getPath();
//	}
//
//	public String uploadFile2(MultipartFile file) throws IOException {
//		StorePath storePath = storageClient.uploadImageAndCrtThumbImage(file.getInputStream(), file.getSize(),
//				FilenameUtils.getExtension(file.getOriginalFilename()), null);
//
//		return storePath.getPath();
//	}
	
//	public String uploadQRCode(MultipartFile file) throws IOException {
//		StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(),
//				"png", null);
//
//		return storePath.getPath();
//	}

	public String uploadQRCode(MultipartFile file) throws IOException {

		return uploadAvatar(file);
	}

	public String uploadBase64(MultipartFile file) throws IOException {
		return uploadAvatar(file);
	}
	
//	public String uploadFace(MultipartFile file) throws IOException {
//		StorePath storePath = storageClient.uploadImageAndCrtThumbImage(file.getInputStream(), file.getSize(),
//				"png", null);
//
//		return storePath.getPath();
//	}
	
//	public String uploadBase64(MultipartFile file) throws IOException {
//		StorePath storePath = storageClient.uploadImageAndCrtThumbImage(file.getInputStream(), file.getSize(),
//				"png", null);
//
//		return storePath.getPath();
//	}
	
	/**
	 * 将一段字符串生成一个文件上传
	 * 
	 * @param content
	 *            文件内容
	 * @param fileExtension
	 * @return
	 */
//	public String uploadFile(String content, String fileExtension) {
//		byte[] buff = content.getBytes(Charset.forName("UTF-8"));
//		ByteArrayInputStream stream = new ByteArrayInputStream(buff);
//		StorePath storePath = storageClient.uploadFile(stream, buff.length, fileExtension, null);
//		return storePath.getPath();
//	}

	// 封装图片完整URL地址
//	private String getResAccessUrl(StorePath storePath) {
//		String fileUrl = AppConstants.HTTP_PRODOCOL + appConfig.getResHost() + ":" + appConfig.getFdfsStoragePort()
//				+ "/" + storePath.getFullPath();
//		return fileUrl;
//	}

	/**
	 * 删除文件
	 * 
	 * @param fileUrl
	 *            文件访问地址
	 * @return
	 */
//	public void deleteFile(String fileUrl) {
//		if (StringUtils.isEmpty(fileUrl)) {
//			return;
//		}
//		try {
//			StorePath storePath = StorePath.parseFromUrl(fileUrl);
//			storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
//		} catch (FdfsUnsupportStorePathException e) {
//			e.getMessage();
//		}
//	}
}
